package com.digitalchina.lxnsy.viewholder;

import android.app.Activity;
import androidx.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.EditAddressActivity;
import com.digitalchina.lxnsy.entity.AddressVO;
import com.digitalchina.lxnsy.interfaces.IAddressCallBack;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.viewholder
 * 创建人：byd
 * 创建时间：2017/3/28 14:28
 * 修改备注：
 * ================================================
 */

public class MyAddressListHolder extends BaseViewHolder<AddressVO> {

    private TextView tvName;
    private TextView tvPhone;
    private TextView tvAddress;
    private TextView tvDefault;
    private Button rbChooseAddress;
    private LinearLayout btnEdit;
    private LinearLayout btnDelete;
    private LinearLayout llCheckBox;
    private boolean isShowCheck = false;
    private IAddressCallBack mCallback;
    private String provinceName, cityName, countyName, detailsName, proCode, cityCode;


    public MyAddressListHolder(ViewGroup parent, boolean isShowCheck, IAddressCallBack mCallback) {
        super(parent, R.layout.item_my_address);
        this.isShowCheck = isShowCheck;
        this.mCallback = mCallback;
        tvName = $(R.id.item_address_tv_name);
        tvPhone = $(R.id.item_address_tv_phone);
        tvAddress = $(R.id.item_address_tv_details);
        tvDefault = $(R.id.item_address_tv_default);
        rbChooseAddress = $(R.id.item_address_bt_choose_address);
        btnEdit = $(R.id.item_address_ll_edit);
        btnDelete = $(R.id.item_address_ll_delete);
        llCheckBox = $(R.id.item_address_ll_choose_address);
    }

    @Override
    public void setData(final AddressVO data) {
        super.setData(data);


        // 选择地址的单选button
        if (isShowCheck)
            llCheckBox.setVisibility(View.VISIBLE);
        else
            llCheckBox.setVisibility(View.GONE);


        tvName.setText(data.getUserName());
        tvPhone.setText(data.getMobilePhone());

        if (null == data.getProvince()) {
            provinceName = "";
        } else {
            proCode = data.getProvince().getCode();
            provinceName = data.getProvince().getProvinceName();
        }

        if (null == data.getCity()) {
            cityName = "";
        } else {
            cityCode = data.getCity().getCode();
            cityName = data.getCity().getCityName();
        }

        if (null == data.getCounty())
            countyName = "";
        else
            countyName = data.getCounty().getCountyName();

        if (null == data.getAddress())
            detailsName = "";
        else
            detailsName = data.getAddress();

//        if (!StringUtil.isStrEmpty(proCode) && !StringUtil.isStrEmpty(cityCode)) {
            if (proCode.equals("110000") || proCode.equals("120000") || proCode.equals("310000") || proCode.equals("500000") || (proCode.equals("460000") && cityCode.equals("469000")))
                tvAddress.setText(String.format("%s %s %s", provinceName, countyName, detailsName));
            else
                tvAddress.setText(String.format("%s %s %s %s", provinceName, cityName, countyName, detailsName));
//        }


        btnEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GotoUtil.gotoForResultActivity((Activity) getContext(), EditAddressActivity.class, 99, "editAddress", data);
            }
        });
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MaterialDialog.Builder(getContext())
                        .title("提示")
                        .content("确定要删除该条地址？")
                        .positiveText("确定")
                        .negativeText("取消")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                mCallback.onlinePreviewClick(getAdapterPosition());
                            }
                        })
                        .show();
            }
        });

        llCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallback.onCheckView(getAdapterPosition());
            }
        });

    }

}
